//
//  FANTextView.swift
//  Weibo - Swift
//
//  Created by user on 16/4/25.
//  Copyright © 2016年 user. All rights reserved.
//

import UIKit

class FANTextView: UITextView {

    override var font: UIFont? {
        didSet{
            label.font = self.font
        }
    }
    
    lazy var label:UILabel = {
    
        let label = UILabel()
        
        label.textColor = UIColor.lightGrayColor()
        
        label.font = UIFont.systemFontOfSize(12)
        
//        label.text = "下雨天音乐与辣条更配哟~~"
        
        label.sizeToFit()
        
        return label

    }()
    
    var placeHolder:String?{
        
        didSet{
            
            label.text = placeHolder
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
    
        
        self.addSubview(label)
        
        label.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self).offset(8)
            
            make.leading.equalTo(5)
            
            make.width.equalTo(self).offset(-10)
        }
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewDidChange", name: UITextViewTextDidChangeNotification, object: nil)
    }
    
    func textViewDidChange(){
        
        label.hidden = self.hasText()
    }
    
    deinit{
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

}
